package com.yy.ant.chat.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.yy.ant.sys.service.IUserService;
import com.yy.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.ant.chat.entity.Flock;
import com.yy.ant.chat.entity.Group;
import com.yy.ant.chat.service.IFlockService;
import com.yy.ant.chat.service.IGroupService;
import com.yy.ant.sys.entity.User;
import com.yy.model.ResultMsg;
import com.yy.model.enums.ResultEnum;
import com.yy.utils.SessionUtil;

/**
 * <p>
 * 分组 前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/chat/group")
public class GroupController {
	@Autowired
	private IFlockService iFlockService;
	
	@Autowired
	private IGroupService iGroupService;
	@Autowired
	private IUserService iUserService;
	/**
	 * 根据用户ID 查询自己的分组或者群
	 *
	 * @return
	 */
	@RequestMapping(value = "/getQunZu")
	public ResultMsg getQunZu(@RequestHeader("token") String token, @RequestParam(value = "name") String name) {
		ResultMsg resultMsg=new ResultMsg();
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		if (StringUtils.equalsIgnoreCase(name, "0")) {
			List<Group> list=iGroupService.list(new QueryWrapper<Group>().eq("user_id", sysUser.getId()));
			List<HashMap<String, Object>> lHashMaps=new ArrayList<HashMap<String,Object>>();
			for (Group flock : list) {
				HashMap<String, Object> hashMap=new HashMap<String, Object>();
				hashMap.put("id", flock.getId());
				hashMap.put("name", flock.getGroupname());
				lHashMaps.add(hashMap);
			}
			resultMsg.setData(lHashMaps);
		}else {
			List<Flock> list=iFlockService.list(new QueryWrapper<Flock>().eq("userid", sysUser.getId()));
			List<HashMap<String, Object>> lHashMaps=new ArrayList<HashMap<String,Object>>();
			for (Flock flock : list) {
				HashMap<String, Object> hashMap=new HashMap<String, Object>();
				hashMap.put("id", flock.getId());
				hashMap.put("name", flock.getGroupname());
				lHashMaps.add(hashMap);
			}
			resultMsg.setData(lHashMaps);
		}
		
		return resultMsg;
	}
	
	/**
	 * 根据用户ID 查询自己的分组
	 *
	 * @return
	 */
	@RequestMapping(value = "/getUserById")
	public ResultMsg getUserById(@RequestHeader("token") String token) {
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		List<Group> list=iGroupService.list(new QueryWrapper<Group>().eq("user_id", sysUser.getId()));
		List<HashMap<String, Object>> lHashMaps=new ArrayList<HashMap<String,Object>>();
		for (Group flock : list) {
			HashMap<String, Object> hashMap=new HashMap<String, Object>();
			hashMap.put("id", flock.getId());
			hashMap.put("name", flock.getGroupname());
			lHashMaps.add(hashMap);
		}
		ResultMsg resultMsg=new ResultMsg();
		resultMsg.setData(lHashMaps);
		return resultMsg;
	}
	
	/**
	 *创建分组
	 *
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ResultMsg add(@RequestHeader("token") String token,@RequestParam(value = "name") String name) {
		ResultMsg resultMsg=new ResultMsg();
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		Group group=new Group();
		group.setGroupname(name);
		group.setUserId(sysUser.getId());
		boolean b=iGroupService.save(group);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		
		return resultMsg;
	}
	
	
}
